package com.zn.services.impl;

import com.zn.dao.*;
import com.zn.pojo.MhyMedia;
import com.zn.pojo.PageQueryBean;
import com.zn.pojo.UnifiedReportData;
import com.zn.pojo.android.*;
import com.zn.pojo.android.dto.VehicleBeanDTO;
import com.zn.services.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class VehicleServiceImpl implements VehicleService {

    @Resource
    private MhyProcessNoInfoMapper processNoMapper;
    @Resource
    private MhySmokeDetectionMapper mhySmokeDetectionMapper;
    @Resource
    private MhyMediaMapper mhyMediaMapper;
    @Resource
    private VehicleInfoMapper vehicleInfoMapper;
    @Resource
    private MhyProcessDataInfoMapper mhyProcessDataInfoMapper;
////    @Autowired
//    private VehicleNgExtraInfoMapper vehicleNgExtraInfoMapper;

    @Override
    public int add(BaseMsgUpload baseMsgUpload) {
        int count =  vehicleInfoMapper.insert(baseMsgUpload.getVehicle());
//        List<NgExtra> extra = baseMsgUpload.getVehicle().getNgExtra();
//        for (NgExtra ngExtra : extra) {
//            ngExtra.setVehicleId(baseMsgUpload.getVehicle().getVehicleId());
//            vehicleNgExtraInfoMapper.insert(ngExtra);
//        }
        return count;
    }

    @Override
    public List<VehicleBeanDTO> getAll() {
        return vehicleInfoMapper.selectAll();
    }

    @Override
    public UnifiedReportData assembleData(Integer vId) {
        VehicleBean vehicleBean = vehicleInfoMapper.selectByKey(vId);
        MhyMedia mhyMedia = mhyMediaMapper.selectByUUId(vehicleBean.getUuid());
        SmokeDetection smokeDetection = mhySmokeDetectionMapper.selectByUUId(vehicleBean.getUuid());
        ProcessNo processNo =  processNoMapper.selectByUUId(vehicleBean.getUuid());
        List<ProcessData> list = null;
        if(!ObjectUtils.isEmpty(smokeDetection) && !StringUtils.isEmpty(smokeDetection.getProcessNo())){
            list = mhyProcessDataInfoMapper.selectByProcessNo(smokeDetection.getProcessNo());
        }
        changeMhyMediaToPath(mhyMedia);
        return new UnifiedReportData(vehicleBean,list,processNo,smokeDetection,mhyMedia);
    }

    @Override
    public VehicleBean getByProcessNo(String processNo) {
        return vehicleInfoMapper.selectByProcessNo(processNo);
    }

    @Override
    public PageQueryBean listVehicle(PageQueryBean pqb) {
        return null;
    }

    @Override
    public VehicleBean getByMhyPlate(String mhyPlate) {
        return vehicleInfoMapper.selectByMhyPlate(mhyPlate);
    }

    @Override
    public List<VehicleBeanDTO> getByProcessNoComOn(String comOn) {
        return vehicleInfoMapper.selectByProcessNoComOn(comOn);
    }

    @Override
    public List<VehicleBean> getAllVehicleInfo() {
        return vehicleInfoMapper.selectAllVehicleInfo();
    }


    private void changeMhyMediaToPath(MhyMedia mhyMedia){
        if(ObjectUtils.isEmpty(mhyMedia)){
            return;
        }
        mhyMedia.setfPic1(changePath(mhyMedia.getfPic1()));
        mhyMedia.setfPic2(changePath(mhyMedia.getfPic2()));
        mhyMedia.setfPic3(changePath(mhyMedia.getfPic3()));
        mhyMedia.setfPic4(changePath(mhyMedia.getfPic4()));
        mhyMedia.setfPic5(changePath(mhyMedia.getfPic5()));
        mhyMedia.setfPic6(changePath(mhyMedia.getfPic6()));
        mhyMedia.setfPic7(changePath(mhyMedia.getfPic7()));
        mhyMedia.setfPic8(changePath(mhyMedia.getfPic8()));
        mhyMedia.setfPic9(changePath(mhyMedia.getfPic9()));
        mhyMedia.setfEtPic1(changePath(mhyMedia.getfEtPic1()));
        mhyMedia.setfEtPic2(changePath(mhyMedia.getfEtPic2()));
        mhyMedia.setfEtPic3(changePath(mhyMedia.getfEtPic3()));
        mhyMedia.setfFrzsyyzz(changePath(mhyMedia.getfFrzsyyzz()));
    }

    private String changePath(String path) {
        if(StringUtils.isEmpty(path)){
            return null;
        }else{
            return "/path/"+path;
        }
    }

}
